<?php

namespace App\Game\Logic;

use App\Game\Core\AStrategy;
use App\Game\Core\Djs;
use App\Game\Core\Message;
use App\Game\Conf\SubCmd;
use App\Game\Core\Room;
use App\Model\User;
use App\Service\Lock;

/**
 *  换桌
 */
class ChangeRoom extends AStrategy
{
    public function exec()
    {
        $account = $this->_params['data']['account'];
        //防止重复操作
        if (!Lock::lock($account . '_change_room', 3)) {
            return;
        }
        $res = Djs::checkScore($account);
        if (!$res) {
            $msg = ['account' => $account, 'room_no' => null, 'status' => 2, 'info' => '积分不足'];
            Message::pushDataToUser($account, $msg, SubCmd::SUB_GAME_ROOM_CREATE_RESP, 0);
            return;
        }

        $room_no = Room::getRoomNoByAccount($account);
        if ($room_no) {
            removeGamePrepareByAccount($room_no, $account);
            removeRoomChairByAccount($room_no, $account);
            removeRoomUserDataByAccount($room_no, $account);
            removeRoomChairByAccount($room_no, $account);
            removeRoomWaitByAccount($room_no, $account);
            removeRoomMapByAccount($account);
        }
//            ddNode('j-1');
        // 快速游戏没有房间号，检查正在等待的房间
//            $wait = $this->getWaitingRoom();
        $wait = Room::getWaitRoom();
        if (empty($wait)) {
            // 创建房间号
            $room_no = Room::createRoomNo();
            // uid=>room_no映射
            Room::addUserRoomMap($account, $room_no);
            // room_no=>[uid1,uid2]
            Room::addRoomWait($room_no, $account);

            setGameProgress($room_no, GameProgress::GAME_JOIN);

            Room::addRoomChair($account, $room_no);

            // 所有房间集合
            Room::addRoomTotal($room_no);


//            $userInfo = User::userInfo($account);
//            $is_real = 1;
//            if ($is_join) {
//                $is_real = 0;
//            }
//            $msg = ['room_no' => $room_no, 'is_real' => $is_real, 'player' => $userInfo, 'status' => 0, 'info' =>
//                '创建成功', 'chair_id' => 1];
//            $data = Packet::packFormat('OK', 1, $msg);
//            $data = Packet::packEncode($data, MainCmd::CMD_SYS, SubCmd::SUB_GAME_ROOM_CREATE_RESP);
//            $serv->push($fd, $data, WEBSOCKET_OPCODE_BINARY);//向用户广播
//            //快速开始途径调用的创建房间，才能添加机器人
//            if ($is_join) {
//                //设置游戏进度类型为单机
//                setGameProgressType($room_no, 1);
//                return;
//            }
//            //推入等待队列
//            $playersData[] = $account;
//            Room::createWaitRoom($room_no, $playersData);
//            return;
        }
//        $room_no = $wait['room_no'];
//        $waiters = getRoomWait($room_no);
//        if (count($waiters) == 0 || count($waiters) == 4) { //没人或者满员调用创建房间
//            $flag = false;
//            //继续尝试3次，如果还是没有等待队列则调用创建
//            for ($i = 0; $i < 3; $i++) {
//                $wait = Room::getWaitRoom();
//                if (empty($wait)) {
//                    continue;
//                }
//                $room_no = $wait['room_no'];
//                $waiters = getRoomWait($room_no);
//                if (count($waiters) == 0 || count($waiters) == 4) {
//                    continue;
//                }
//                $flag = true;
//                break;
//            }
//            if (!$flag) {
//                $task = container()->get(CreateRoomTask::class);
//                $task->createRoom($fd, $account, true);
//                return;
//            }
//        }
//        $this->roomLockTrans($room_no, $account);
//        $waiters = getRoomWait($room_no);
//        if (count($waiters) < 4) { //人数不够继续回归队列
//            Room::addWaitRoom($room_no, $waiters);
//        }
    }

    /**
     * 锁操作房间数据
     * @param $serv
     * @param $room_no
     * @param $account
     */
    public
    function roomLockTrans($room_no, $account)
    {
        Room::addUserRoomMap($account, $room_no);      // 绑定用户和房间号
        Room::addRoomUser($account, $room_no);         // 将新加入用户推入等待房间
        Room::addRoomChair($account, $room_no);         // 将新加入用户推入等待房间


//        $players = $this->checkWaitMembers($room_no, $account);  //检查房间人数
//        if (!empty($players['players'])) {
//            $prepareArr = getGamePrepareALL($room_no);
//            $chairs = getRoomChairs($room_no);
//            foreach ($players['players'] as $k => $v) {
////                        dd('players v', $v);
//                $players['players'][$k]['chair_id'] = $chairs[$players['players'][$k]['account']];
//                $players['players'][$k]['nickname'] = filterNickname($v['nickname']);
//                $players['players'][$k]['prepare'] = isset($prepareArr[$players['players'][$k]['account']]) ? 1 : 0;
//            }
//        }
//        $room_data = ['status' => 'waiting', 'players' => $players, 'room_no' =>
//            $room_no, 'is_real' => 1];
//        Message::broadcast($room_no, $room_data, SubCmd::SUB_GAME_WAIT);
    }


}